(function () {
    Codestrong.ui.CreateLectureBySubject= function (_args) {


	var aps='';
	if(Ti.Platform.osname== 'android') {
  		 Titanium.include('../globals.js');
  		 aps='../';
    }
    else {
   		  Titanium.include('globals.js');
    }
	/////////////////////EVENT//////////
	var data = []; 
	var response;
	


	var win = Titanium.UI.createWindow({
            id: 'lectureWindow1',
            title: _args.TenMonHoc,
            backgroundColor: '#FFF',
            barColor: '#414444',
            fullscreen: false,
            modal: true,
    });
      
    var egc = new globals.EventGarbageCollection(win);
    egc.addEventListener('back_LectureBySubject', function () {
 		
 		Codestrong.navGroup.close(win, {
                        animated: true
        });
        
	});
    egc.addEventListener('LectureBySubject:CreateLectureDetail', function(data) {
    
	  Codestrong.navGroup.open(Codestrong.ui.CreateLectureDetail({
			LectureId:data.LectureId,
			TypeID:data.TypeID,
			LectureName:data.LectureName,
			TenMonHoc:_args.TenMonHoc,
			Price:data.Price,
			TeacherImage:data.TeacherImage,
			IsSee:data.IsSee,
		    IsDownload:data.IsDownload,
		    dataLecture:data.dataLecture,
		    dataLectureArray:data.dataLectureArray	,  		
		}), 
		{
		    animated: true,
		    navBarHidden:true
		});	
					
		
    })
    
    
        
	////////////main_header///////////////////////////
	 var databutton_right=[
    	{Id:1,Image:aps+'images/list_bullets.png',Image_:aps+'images/list_bullets_.png',tag:'Category'},
	    {Id:2,Image:aps+'images/account.png',Image_:aps+'images/account_.png',tag:'Login'},
	    {Id:3,Image:aps+'images/search.png',Image_:aps+'images/search_.png',tag:'Search'},
	    
	]
	var main_header=require('controls/main_header').main_header({
	 		backgroundImage: aps+'images/panel3.png',
	 		headertext:'Thầy trò Mobile: '+_args.TenMonHoc,
		    left: 0,
		    top: 0,
		    height:65,
		    width: '100%',//Globals.platformWidth,
		    touchEnabled: true		,
		    databutton_right:databutton_right,
		   
		    imback:   aps+'images/back.png',
		    imback_:   aps+'images/back_.png',
		    idWinParent:'LectureBySubject',
    })

	win.add(main_header);
	
	
	var ScrollViewMain = Ti.UI.createScrollView({
		top:main_header.top+main_header.height,
		//height:'100%',
		touchEnabled:true,
	});
	
	var lbLoading = Ti.UI.createLabel({
		text:Globals.loading,
		color:'#1e6cc1',
		font:{
			fontSize:Ti.App.Properties.getString('fontsizeLarge'),
			fontWeight:'bold'
		},
	});
	ScrollViewMain.add(lbLoading)
	
	
	
	var PopularData = function(dataMon) {
		if(dataMon.length==0){lbLoading.text=Globals.lbnodata;}
		else {lbLoading.text='';}
		var Items=dataMon;
		var top_t=0
		var IsMod=0
		var lengthData=Items.length
	
		if(Items.length%2!=0)
		{
			IsMod=1;
			lengthData++;
		}
			
		
		
		for(var i = 0; i < lengthData; i++) {
		   if(i==lengthData-1 && IsMod==1)
		   {
		   		 var viewrightfinal = Ti.UI.createView({
						height:113,
						width:Titanium.Platform.displayCaps.platformWidth/2,
						right:0,
						top:top_t,
						backgroundColor:'transparent',
						//backgroundImage:_args.backgroundImageTable,
						borderRadius:0,
						borderColor:'black',
						borderWidth:1,
						touchEnabled:false,
				});
				ScrollViewMain.add(viewrightfinal);
		   }
		   else
		   {
		   		
		   		
		   		 var viewpanel=require('controls/LectureBySubject_table').LectureBySubject_table({
			  			height:113,
						top:top_t,
						text:Items[i].Name,
						image:aps+'images/teacher/'+Items[i].TeacherImage,//aps+'images/video.png',
						backgroundColorSection:'#2981a3',
						id:Items[i].Id,
						Price:Items[i].Price,
						start:aps+'images/4start.png',
						
						LectureId:Items[i].Id,
						TypeID:Items[i].IdLop,
						LectureName:Items[i].Name,
						TeacherImage:Items[i].TeacherImage,
						IsSee:Items[i].IsSee,
						IsDownload:Items[i].IsDownload,
						dataLecture:Items[i],
						dataLectureArray:dataMon	,  		
				    })
						
				 ScrollViewMain.add(viewpanel);
				
						
				 if(i%2==0)
				 {
				 	viewpanel.left=0;
				 }
				 else
				 {
				 	viewpanel.right=0;
				   	top_t=top_t+viewpanel.height;
				 }   
			}
		}
	}
	
	
	
	
	
	win.add(ScrollViewMain);
	
	
	
	
	 /////////////////////////////FUNCTION///////////////////////
  	var dataMon=[]
    var getTableData_byMonHoc = function(IDMon,TenMonHoc) {
    	var jsonurl=Globals.serviceurl_GetListLecture;
    	var param = {        
			  "IDMon" : IDMon,
			  "SearchName":'SearchName',
			  "UserID":Ti.App.Properties.getInt('login_username_id')
			};
    	require('settings').GetJson(jsonurl,param,'LectureBySubject_onload_table','LectureBySubject_error_even');
    	
    	
    	
	/*
			var row = null;
			var data = [];
			var param = {        
			  "IDMon" : IDMon,
			  "SearchName":'SearchName',
			  "UserID":Ti.App.Properties.getInt('login_username_id')
			};
			var xhr = Titanium.Network.createHTTPClient({
		   
				   onload : function(e) 
				   {
				       Titanium.API.info(this.responseText);
				       
				       //do whatever you want to do with your response
				   
				
						 json = this.responseText;
						 json=json.replace('<string xmlns="http://tempuri.org/">', '');
						 json=json.replace('<?xml version="1.0" encoding="utf-8"?>', '');
						 json=json.replace('</string>', '');
					
						 response = JSON.parse(json);
						
						
				
						 
					 	for (i = 0; i < response.OBJECT.length; i++) {
							dataMon[i] =response.OBJECT[i];
						}
						
						
						PopularData(dataMon)
						Codestrong.ui.activityIndicator.hideModal();
						
						
				   }, 
				   onerror : function(e) {
				   	 alert('Loi trong ham error LectureBySubject')
				      Codestrong.ui.activityIndicator.hideModal();
				      lbLoading.text=Globals.loadingTimeout;
				   },
				   timeout : Globals.timeout //5000
			});
			xhr.open("POST",Globals.serviceurl_GetListLecture);
			xhr.send(param);
			*/
		};
	

	egc.addEventListener('LectureBySubject_onload_table', function (e) {
 		
 		var datajson=e.datajson;
 		
		PopularData(datajson)
		Codestrong.ui.activityIndicator.hideModal();
	});
	egc.addEventListener('LectureBySubject_error_even', function (e) {
 		
 		 lbLoading.text=Globals.loadingTimeout;
 		 Codestrong.ui.activityIndicator.hideModal();
	});
		

	/////////////////////////OPEN WINDOWN/////////////////////////
	
	
	 if (Ti.Network.online) {
			Codestrong.ui.activityIndicator.showModal(Globals.loading, Globals.timeout, Globals.loadingTimeout);
			getTableData_byMonHoc(_args.IDMon,_args.TenMonHoc)
			
	 }
	 else
	 {
	  	 
	  	    lbLoading.text=Globals.connecterror;
	 }

 	return win;
       
    };
})();